﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gizmox.WebGUI.Forms;
using GuiLabs.Undo;
using Bronze.ReportBuilder.BusinessComponent;

namespace Bronze.ReportBuilder.Actions
{
    public class CreateAction : AbstractAction
    {
        protected readonly Control _control;
        protected readonly FormDesigner _designer;

        public CreateAction(Control control, FormDesigner designer)
        {
            this._control = control;
            this._designer = designer;
        }

        protected override void ExecuteCore()
        {
            _designer.Controls.Insert(0, _control);
            _designer.SelectControl(_control, true);
        }

        protected override void UnExecuteCore()
        {
            var sel = _designer.GetSelection(_control);
            if (sel != null)
            {
                _designer.Controls.Remove(sel);
            }
            _designer.Controls.Remove(_control);
        }
    }


    public class DeleteAction : AbstractAction
    {
        protected readonly FormDesigner _designer;
        private int[] _indexs;
        protected List<int> _delControlsIndex;
        readonly List<Control> _delControls=new List<Control>();

        public DeleteAction(FormDesigner designer, params Control[] controls)
        {
            for (int i = 0; i < designer.Controls.Count; i++)
            {
                if (controls.Contains(designer.Controls[i]))
                {
                    _delControls.Add(designer.Controls[i]);
                }
            }

            this._designer = designer;
        }

        protected override void ExecuteCore()
        {
            if (_delControls==null)
            {
                return;
            }
            var selects = _designer.GetSelections();
            foreach (var selectionBox in selects)
            {
                _designer.Controls.Remove(selectionBox);
            }

            if (_delControlsIndex == null)
            {
                _delControlsIndex=new List<int>();
                var dels = new List<int>();
                var indexes = new int[_designer.Controls.Count,2];
                for (var i = 0; i < _designer.Controls.Count; i++)
                {
                    if (_delControls.Contains(_designer.Controls[i]))
                    {
                        dels.Add(i);
                    }
                    indexes[i, 0] = i;
                    indexes[i, 1] = i;
                }


                Action reorder = () =>
                                     {
                                         var current = 0;
                                         for (var i = 0; i < indexes.GetLength(0); i++)
                                         {
                                             if (dels.Contains(indexes[i, 1]))
                                             {
                                                 indexes[i, 1] = current - 1;
                                             }
                                             else
                                             {
                                                 current++;
                                             }
                                         }
                                     };

                reorder();

                for (var i = dels.Count - 1; i >= 0; i--)
                {
                    var lastIndex = dels[i];
                    var resultIdx = indexes[lastIndex, 1] + 1;
                    _delControlsIndex.Insert(0, resultIdx);
                    indexes[lastIndex, 1] = resultIdx;
                    dels.Remove(lastIndex);
                    reorder();
                }
            }

            //删除
            foreach (var c in _delControls)
            {
                _designer.Controls.Remove(c);
            }
        }

        protected override void UnExecuteCore()
        {
            if (_delControls == null)
            {
                return;
            }
            for (var i = _delControls.Count-1; i >=0; i--)
            {
                _designer.Controls.Insert(_delControlsIndex[i], _delControls[i]);
            }
            foreach (var c in _delControls)
            {
                _designer.SelectControl(c, true);
            }

        }
    }

    public class SaveWizardAction:DeleteAction
    {
        List<WizardStep> Wizards;
        List<WizardStep> tempWizards;
        protected readonly Control[] addControls;

        public SaveWizardAction(FormDesigner designer, List<WizardStep> wizards,  Control[] addControls ,  Control[] removeControls) :
            base(designer, removeControls)
        {
            this.addControls = addControls;
            this.Wizards = wizards;
        }

        protected override void ExecuteCore()
        {
            tempWizards = _designer.Wizards;
            _designer.Wizards = Wizards;

            base.ExecuteCore();
            foreach (var item in addControls)
            {
                _designer.Controls.Insert(0, item);
                _designer.SelectControl(item, true, (sel) =>
                    {
                        sel.CanManualDelete = false;
                    });
            }
        }

        protected override void UnExecuteCore()
        {
            base._designer.Wizards = tempWizards;
            base.UnExecuteCore();


            foreach (var item in addControls)
            {
                var sel = _designer.GetSelection(item);
                if (sel != null)
                {
                    _designer.Controls.Remove(sel);
                }
                _designer.Controls.Remove(item);
            }
        }
    }
}